package com.panda.utils;

import com.panda.command.Console;
import com.panda.pojo.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class MappingUtils {


    //可以是单向映射也可以是双向映射
    private static final String regex = ("[ \n]*([a-zA-Z][a-zA-Z0-9]*[ \n]*((->)|(<->)|(<-))[ \n]*[a-zA-Z][a-zA-Z0-9]*([ \n]*,[ \n]*[a-zA-Z][a-zA-Z0-9]*[ \n]*((->)|(<->)|(<-))[ \n]*[a-zA-Z][a-zA-Z0-9]*)*)?[ \n]*");

    private static final Pattern pattern = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)[ \n]*((->)|(<->)|(<-))[ \n]*([a-zA-Z][a-zA-Z0-9]*)");

    static void mapping(Component element) {

        String text = element.getBodyText();

        if (text != null)
            if (text.matches(regex)) {

                if (!text.trim().equals("")) {

                    Matcher matcher = pattern.matcher(text);

                    StringBuilder builder = new StringBuilder();

                    String script = element.getScope().getScript();
                    //禁用__real_parent__变量
                    if (script != null) {
                        script = script.replace("__real_parent__", "");
                    }

                    boolean have = false;

                    while (matcher.find()) {

                        String son = matcher.group(6);

                        if (element.getCanMappingScopeParams().contains(son)) {

                            have = true;

                            String fa = matcher.group(1);

                            String bind = matcher.group(2);


                            if (bind.contains(">")) {

                                builder.append("\n\n__real_parent__.$watch('").append(fa).append("', function (").append(fa).append(") {if ($scope.")
                                        .append(son).append("!==").append(fa).append("){$scope.").append(son).append("=").append(fa).append(";}});\n");


                            }

                            if (bind.contains("<")) {

                                builder.
                                        append("\n\n$scope.$watch('").append(son).append("', function (").append(son).append(") {if (__real_parent__.").append(fa).
                                        append("!==").append(son).append("){__real_parent__.").append(fa).append("=").append(son).append(";}});\n");

                            }

                            builder.append("\n");


                            if (script == null) {
                                script = builder.toString();

                            } else {
                                script = script + builder.toString();
                            }

                            builder.delete(0, builder.length());

                        } else {

                            Console.warning("无法映射组件内不存在或私有域变量: " + son);

                        }

                    }


                    if (have) {

                        /*

                        //将会影响正确性?原因不明
                        if (__real_parent__.$$childHead === $scope) {__real_parent__.$$childHead = $scope.$$nextSibling;}

                         */
                        //需要修改angularjs的虚拟dom树
                        element.getScope().setScript(("__real_parent__ = $scope.$parent;$scope.$parent = $rootScope;__childTail__ = $rootScope.$$childTail;__childTail__.$$nextSibling = $scope;$rootScope.$$childTail = $scope;$scope.$$prevSibling = __childTail__;if (__real_parent__.$$childHead === $scope) { __real_parent__.$$childHead = $scope.$$nextSibling;}__real_parent__.$$childTail = $scope.$$nextSibling;\n" + script).replaceAll("[\n]+", "\n"));
                    }

                }

            } else {
                throw new RuntimeException("不规则映射表:" + text);
            }

    }


}
